package com.xpgk.mvc.statistic.measure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Record;
import com.xpgk.constant.ConstantSplitPage;
import com.xpgk.dto.SplitPage;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.scallVisit.ScallVisitXLS;
import com.xpgk.mvc.user.User;

public class MeasureStatisticController extends BaseController {
	
	public static final String sqlId_splitPageSelect = "sys.measureStatic.splitPageSelect";
	
	public static final String sqlId_splitPageFrom = "sys.measureStatic.splitPageFrom";
	
	public void index() {
		setAttr("curOrgan", Organization.dao.findById(getCUserOrganIds()));
		render("measureStatistic.html");
	}
	
	public void view(){
		String organMangerIds = (String) splitPage.getQueryParam().get("organMangerIds");
		if(null != organMangerIds && !organMangerIds.equals("-1")){
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("column", Organization.column_mangerIds);
			String sql = getSqlByBeetl(Organization.sqlId_column, param);
			List<Organization> organs = Organization.dao.find(sql, organMangerIds);
			if(organs.size() > 0){
				List<String> bhs = new ArrayList<String>();
				int bhLength = 0;
				for(Organization organ : organs){
					String bh = organ.getStr(Organization.column_bh);
					bhLength = bh.length();
					bhs.add(bh);
				} 
				splitPage.getQueryParam().remove("selOrganIds");
				splitPage.getQueryParam().put("bhs", bhs);
				splitPage.getQueryParam().put("bhLength", String.valueOf(bhLength));
			}
		}
		String json = paging(splitPage, sqlId_splitPageSelect, sqlId_splitPageFrom);
		System.out.println(json);
		renderJson(json);
		
	}
	
	public void excel(){
		System.out.println("==================");
		String startTime = getPara("_query.startTime");
		String endTime = getPara("_query.endTime");
		String selOrganIds = getPara("_query.selOrganIds");
		String organMangerIds = getPara("_query.organMangerIds");
		String gkfl = getPara("_query.gkfl");
		String jdfs = getPara("_query.jdfs");
		String gkfy = getPara("_query.gkfy");
		SplitPage splitPage = new SplitPage();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("startTime", startTime);
		param.put("endTime", endTime);
		param.put("selOrganIds", selOrganIds);
		param.put("organMangerIds", organMangerIds.equals("-1")?"":organMangerIds);
		param.put("gkfl", gkfl.equals("-1")?"":gkfl);
		param.put("jdfs", jdfs.equals("-1")?"":jdfs);
		param.put("gkfy", gkfy.equals("-1")?"":gkfy);
		splitPage.setQueryParam(param);
		splitPage.setPageSize(ConstantSplitPage.default_maxSize);
		String json = paging(splitPage, sqlId_splitPageSelect, sqlId_splitPageFrom);
		
		//搜索条件
		Organization  organ=Organization.dao.findById(selOrganIds);
		User  fzruser=User.dao.findById(organMangerIds);
		Code  gkflcode=Code.dao.findById(gkfl);
		Code  jdfscode=Code.dao.findById(jdfs);
		Code  gkfycode=Code.dao.findById(gkfy);
		String fzruserstr=fzruser==null?"":(",顾客负责人："+fzruser.getStr(User.column_name));
		String gkflcodestr=gkflcode==null?"":(",顾客分类："+gkflcode.getStr(Code.column_name));
		String jdfscodestr=jdfscode==null?"":(",进店方式："+jdfscode.getStr(Code.column_name));
		String gkfycodestr=gkfycode==null?"":(",顾客反映："+gkfycode.getStr(Code.column_name));
		
		String  search="日期："+startTime+"~"+endTime+",门店："+organ.getStr(Organization.column_name)+
				fzruserstr+gkflcodestr+jdfscodestr+gkfycodestr+",导出人员："+getCUser().getStr(User.column_name);
		
		render(new MeasureStatisticXLS((List<Record>)splitPage.getList(),search, "咨询测算查询表"));
	}
	

}
